//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "SpreadedOptionletVolatility.h"
using namespace Cephei::QL::Termstructures::Volatility::Optionlet;
#include <gen/QL/Times/Calendar.h>
#include <gen/QL/Times/DayCounter.h>
#include <gen/QL/Termstructures/Volatility/Optionlet/OptionletVolatilityStructure.h>
#include <gen/QL/Quote.h>
#include <gen/QL/Times/Period.h>
#include <gen/QL/Termstructures/Volatility/SmileSection.h>
using namespace Cephei::QL::Times;
using namespace Cephei::QL;
using namespace Cephei::QL::Termstructures::Volatility;
#define HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ baseVol, Cephei::QL::IQuote^ spread) : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
    COptionletVolatilityStructure^ _CbaseVol;
    CQuote^ _Cspread;
    try
    {
#ifdef HANDLE
        _phSpreadedOptionletVolatility = NULL;
#endif
        _CbaseVol = safe_cast<COptionletVolatilityStructure^> (baseVol);
        _CbaseVol->Lock();
        Handle<QuantLib::OptionletVolatilityStructure>& _baseVol = static_cast<Handle<QuantLib::OptionletVolatilityStructure>&> (_CbaseVol->GetHandle ()); 
        _Cspread = safe_cast<CQuote^> (spread);
        _Cspread->Lock();
        Handle<QuantLib::Quote>& _spread = static_cast<Handle<QuantLib::Quote>&> (_Cspread->GetHandle ()); 
        _ppSpreadedOptionletVolatility = new boost::shared_ptr<QuantLib::SpreadedOptionletVolatility> (new QuantLib::SpreadedOptionletVolatility ( _baseVol,  _spread ));
        SetOptionletVolatilityStructure (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_CbaseVol != nullptr) _CbaseVol->Unlock();
        if (_Cspread != nullptr) _Cspread->Unlock();
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (boost::shared_ptr<QuantLib::SpreadedOptionletVolatility>& childNative, Object^ owner) : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phSpreadedOptionletVolatility = NULL;
#endif
	_ppSpreadedOptionletVolatility = &childNative;
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
}
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (QuantLib::SpreadedOptionletVolatility& childNative, Object^ owner) : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phSpreadedOptionletVolatility = NULL;
#endif
	_ppSpreadedOptionletVolatility = new boost::shared_ptr<QuantLib::SpreadedOptionletVolatility> (&childNative);
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
    _SpreadedOptionletVolatilityOwner = owner;
    _OptionletVolatilityStructureOwner = owner;
}

Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (CSpreadedOptionletVolatility^ copy) : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phSpreadedOptionletVolatility = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppSpreadedOptionletVolatility = new boost::shared_ptr<QuantLib::SpreadedOptionletVolatility> (copy->GetShared());
        _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (PLATFORM::Type^ t) : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phSpreadedOptionletVolatility = NULL;
#endif
	if (!t->IsSubclassOf(CSpreadedOptionletVolatility::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (QuantLib::Handle<QuantLib::SpreadedOptionletVolatility>& childNative, Object^ owner)  : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
	_phSpreadedOptionletVolatility = &childNative;
	_ppSpreadedOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::SpreadedOptionletVolatility>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
    _SpreadedOptionletVolatilityOwner = owner;
}
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (QuantLib::Handle<QuantLib::SpreadedOptionletVolatility> childNative)  : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
	_phSpreadedOptionletVolatility = &childNative;
	_ppSpreadedOptionletVolatility = &static_cast<boost::shared_ptr<QuantLib::SpreadedOptionletVolatility>>(childNative.currentLink());
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
}
#endif
#ifdef STRUCT
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::CSpreadedOptionletVolatility (QuantLib::SpreadedOptionletVolatility childNative)  : COptionletVolatilityStructure(CSpreadedOptionletVolatility::typeid)
{
#ifdef HANDLE
	_phSpreadedOptionletVolatility = NULL;
#endif
	_ppSpreadedOptionletVolatility = new boost::shared_ptr<QuantLib::SpreadedOptionletVolatility> (new QuantLib::SpreadedOptionletVolatility (childNative));
    _ppOptionletVolatilityStructure = new boost::shared_ptr<QuantLib::OptionletVolatilityStructure> (boost::dynamic_pointer_cast<QuantLib::OptionletVolatilityStructure> (*_ppSpreadedOptionletVolatility));
}
#endif

Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::~CSpreadedOptionletVolatility ()
{
    if (_ppSpreadedOptionletVolatility != NULL)
    {
	    delete _ppSpreadedOptionletVolatility;
        _ppSpreadedOptionletVolatility = NULL;
    }
}
Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::!CSpreadedOptionletVolatility ()
{
    if (_ppSpreadedOptionletVolatility != NULL)
    {
	    delete _ppSpreadedOptionletVolatility;
    }
}
QuantLib::SpreadedOptionletVolatility& Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::GetReference ()
{
    if (_ppSpreadedOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return **_ppSpreadedOptionletVolatility;
}
boost::shared_ptr<QuantLib::SpreadedOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::GetShared ()
{
    if (_ppSpreadedOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return *_ppSpreadedOptionletVolatility;
}
QuantLib::SpreadedOptionletVolatility* Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::GetPointer ()
{
    if (_ppSpreadedOptionletVolatility == NULL) throw REFNEW NativeNullException ();
	return &**_ppSpreadedOptionletVolatility;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::SpreadedOptionletVolatility>& Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::GetHandle ()
{
	if (_phSpreadedOptionletVolatility == NULL)
	{
		_phSpreadedOptionletVolatility = new Handle<QuantLib::SpreadedOptionletVolatility> (*_ppSpreadedOptionletVolatility);
	}
	return *_phSpreadedOptionletVolatility;
}
#endif
bool Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::HasNative () 
{
	return (_ppSpreadedOptionletVolatility != NULL);
}

Cephei::QL::Times::ICalendar^ Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::Calendar::get ()
{
    try
    {
    	QuantLib::Calendar _rv = (QuantLib::Calendar)(*_ppSpreadedOptionletVolatility)->calendar ( );   
        Cephei::QL::Times::CCalendar^ _nrv = REFNEW Cephei::QL::Times::CCalendar (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Times::IDayCounter^ Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::DayCounter::get ()
{
    try
    {
    	QuantLib::DayCounter _rv = (QuantLib::DayCounter)(*_ppSpreadedOptionletVolatility)->dayCounter ( );   
        Cephei::QL::Times::CDayCounter^ _nrv = REFNEW Cephei::QL::Times::CDayCounter (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::MaxDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSpreadedOptionletVolatility)->maxDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::MaxStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSpreadedOptionletVolatility)->maxStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::MaxTime::get ()
{
    try
    {
    	QuantLib::Time _rv = (QuantLib::Time)(*_ppSpreadedOptionletVolatility)->maxTime ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::MinStrike::get ()
{
    try
    {
    	QuantLib::Rate _rv = (QuantLib::Rate)(*_ppSpreadedOptionletVolatility)->minStrike ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
DateTime Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::ReferenceDate::get ()
{
    try
    {
    	QuantLib::Date _rv = (QuantLib::Date)(*_ppSpreadedOptionletVolatility)->referenceDate ( );   
        DateTime _nrv = (DateTime)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt32 Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::SettlementDays::get ()
{
    try
    {
    	QuantLib::Natural _rv = (QuantLib::Natural)(*_ppSpreadedOptionletVolatility)->settlementDays ( );   
        UInt32 _nrv = (UInt32)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
QL::Times::BusinessDayConventionEnum Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility::BusinessDayConvention::get ()
{
    try
    {
    	QuantLib::BusinessDayConvention _rv = (QuantLib::BusinessDayConvention)(*_ppSpreadedOptionletVolatility)->businessDayConvention ( );   
        QL::Times::BusinessDayConventionEnum _nrv = (QL::Times::BusinessDayConventionEnum)_rv;
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Termstructures::Volatility::Optionlet::ISpreadedOptionletVolatility^ Cephei::QL::Termstructures::Volatility::Optionlet::CSpreadedOptionletVolatility_Factory::Create (Cephei::QL::Termstructures::Volatility::Optionlet::IOptionletVolatilityStructure^ baseVol, Cephei::QL::IQuote^ spread)
{
    return REFNEW CSpreadedOptionletVolatility ( baseVol,  spread);
}
